package cn.zxy.sqldb;

import cn.zxy.model.User;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class ReturnSql {

    //查询操作
    public String getSelectAll(){
        return new SQL(){
            {
                SELECT("*");
                FROM("user");
            }
        }.toString();
    }

    //动态sql查询模式
//    trends  动态
    public String getTrendsUser(User user){
        return new SQL(){
            {
                SELECT("*");
                FROM("user");
                if (user.getId()!=0){
                    WHERE("id=#{id}");
                }
                if (user.getUsername()!=null && user.getUsername()!=""){
                    WHERE("username=#{username}");
//                    AND().WHERE("username=#{username}");
                }
            }
        }.toString();
    }
//根据名字和地址模糊查询
    public String getLikeUser(User user){
        return new SQL(){
            {
                SELECT("*");
                FROM("user");
                if (user.getUsername()!=null && user.getUsername()!=""){
                    WHERE("username like concat('%',#{username},'%')");
                }
                if (user.getAddress()!=null && user.getAddress()!=""){
                    OR().WHERE("address like concat('%',#{address},'%')");
                }
            }
        }.toString();
    }

    //新增操作
    public String SetInsert(User user){
        return new SQL(){
            {
                INSERT_INTO("user");
                INTO_VALUES("#{id},#{username},#{sex},#{address},#{birthday}");
            }
        }.toString();
    }

    //修改操作
    public String SetUpdate(User user){
        return new SQL(){
            {
                UPDATE("user");
                SET("username=#{username}","sex=#{sex}");
                WHERE("id=#{id}");
            }
        }.toString();
    }

    //动态修改拼接操作
    public String DTUpdate(User user){
        return new SQL(){
            {
                UPDATE("user");
                if (user.getUsername()!=null && user.getUsername()!=""){
                    SET("username=#{username}");
                }
                if (user.getSex()!=null){
                    SET("sex=#{sex}");
                }
                if (user.getAddress()!=null){
                    SET("address=#{address}");
                }
                if (user.getId()!=0){
                    WHERE("id=#{id}");
                }
            }
        }.toString();
    }

    //删除操作
//    public String Delete(){
//        return new SQL(){
//            {
//                DELETE_FROM("user");
//                WHERE("id=#{id}");
//            }
//        }.toString();
//    }


    public String Delete(Integer id){
        System.out.println(id);
        return new SQL(){
            {
                DELETE_FROM("user");
                WHERE("id=#{id}");
                AND().WHERE("address=#{address}");
            }
        }.toString();
    }

    public String DeleteA(Map<String,Object> map){
        System.out.println(map.get("id"));
        return "delete from user where id="+map.get("id")+" and address='"+map.get("address")+"'";
    }










}
